package com.ruoyi.hotel.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.hotel.domain.HlCard;
import com.ruoyi.hotel.domain.HlMember;
import com.ruoyi.hotel.domain.HlMemberRecord;
import com.ruoyi.hotel.domain.HlUser;
import com.ruoyi.hotel.domain.dto.HlMemberRecordDTO;
import com.ruoyi.hotel.domain.export.HlMemberRecordExport;
import com.ruoyi.hotel.domain.vo.MemberRecordVO;
import com.ruoyi.hotel.mapper.HlCardMapper;
import com.ruoyi.hotel.mapper.HlMemberMapper;
import com.ruoyi.hotel.mapper.HlMemberRecordMapper;
import com.ruoyi.hotel.mapper.HlUserMapper;
import com.ruoyi.hotel.service.IHlMemberRecordService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import static com.ruoyi.common.core.domain.AjaxResult.success;
import static com.ruoyi.common.utils.SecurityUtils.getUsername;

/**
 * 会员权限Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-12-10
 */
@Service
public class HlMemberRecordServiceImpl implements IHlMemberRecordService
{

    @Autowired
    private HlMemberRecordMapper hlMemberRecordMapper;


    @Autowired
    private HlUserMapper hlUserMapper;


    @Autowired
    private HlMemberMapper hlMemberMapper;


    @Autowired
    private HlCardMapper hlCardMapper;

    /**
     * 查询会员记录列表
     *
     * @param hlMemberRecord 会员记录
     * @return 会员记录集合
     */
    @Override
    public List<HlMemberRecordDTO> selectHlMemberRecordList(HlMemberRecord hlMemberRecord) {

        List<HlMemberRecordDTO> hlMemberRecords = hlMemberRecordMapper.selectHlMemberRecordList(hlMemberRecord);



        return hlMemberRecords;
    }

    /**
     * 查询会员记录列表Export
     *
     * @param hlMemberRecord 会员记录
     * @return 会员记录集合
     */
    @Override
    public List<HlMemberRecordExport> selectHlMemberRecordExportList(HlMemberRecord hlMemberRecord) {

        List<HlMemberRecordDTO> hlMemberRecordDTOS = hlMemberRecordMapper.selectHlMemberRecordExportList(hlMemberRecord);

//        创建传出的容器
        List<HlMemberRecordExport> hlMemberRecordExports = new ArrayList<>();

        for(HlMemberRecordDTO hlMemberRecordDTO : hlMemberRecordDTOS){
//            创建数据接受的容器
            HlMemberRecordExport hlMemberRecordExport = new HlMemberRecordExport();

//            ===========================================
//            业务的地方1

            hlMemberRecordDTO.getUser().setSex("0".equals(hlMemberRecordDTO.getUser().getSex())?"女" : "男");
            hlMemberRecordDTO.getMember().setIsBreakfast("0".equals(hlMemberRecordDTO.getMember().getIsBreakfast())?"无" : "有");
            hlMemberRecordDTO.getMember().setIsLateCheckout("0".equals(hlMemberRecordDTO.getMember().getIsLateCheckout())?"无" : "有");

            BeanUtils.copyProperties(hlMemberRecordDTO.getUser(),hlMemberRecordExport);
            BeanUtils.copyProperties(hlMemberRecordDTO.getMember(),hlMemberRecordExport);
            hlMemberRecordExport.setState("0".equals(hlMemberRecordDTO.getState())?"曾办理的会员":"现在的会员");

            HlCard hlCard = hlCardMapper.selectHlCardById(hlMemberRecordDTO.getUser().getCardId());
            hlMemberRecordExport.setCardType(hlCard.getCardType());
            hlMemberRecordExport.setRemark(hlMemberRecordDTO.getRemark());
            hlMemberRecordExport.setCreateBy(hlMemberRecordDTO.getCreateBy());
            hlMemberRecordExport.setUpdateBy(hlMemberRecordDTO.getUpdateBy());
            hlMemberRecordExport.setCreateTime(hlMemberRecordDTO.getCreateTime());
            hlMemberRecordExport.setUpdateTime(hlMemberRecordDTO.getUpdateTime());


//            ===========================================
//            业务的地方2过于冗余
//
//            hlMemberRecordExport.setName(hlMemberRecordDTO.getUser().getName());
//            hlMemberRecordExport.setSex("0".equals(hlMemberRecordDTO.getUser().getSex())?"女" : "男");
//            hlMemberRecordExport.setOrigin(hlMemberRecordDTO.getUser().getOrigin());
//            hlMemberRecordExport.setPhone(hlMemberRecordDTO.getUser().getPhone());
//            hlMemberRecordExport.setIdCard(hlMemberRecordDTO.getUser().getIdCard());
//
////            这里用copy工具set也可，可测试一下
////            这里可以进行sql优化，需要调整为子查询，后面再考虑修改
//            HlCard hlCard = hlCardMapper.selectHlCardById(hlMemberRecordDTO.getUser().getCardId());
//            hlMemberRecordExport.setCardType(hlCard.getCardType());
//
//            hlMemberRecordExport.setMemberType(hlMemberRecordDTO.getMember().getMemberType());
//            hlMemberRecordExport.setIsBreakfast("0".equals(hlMemberRecordDTO.getMember().getIsBreakfast())?"无" : "有");
//            hlMemberRecordExport.setIsLateCheckout("0".equals(hlMemberRecordDTO.getMember().getIsLateCheckout())?"无" : "有");
//            hlMemberRecordExport.setDiscount(hlMemberRecordDTO.getMember().getDiscount());
//            hlMemberRecordExport.setDiscount(hlMemberRecordDTO.getMember().getDiscount());
//            hlMemberRecordExport.setMemberPrice(hlMemberRecordDTO.getMember().getMemberPrice());
//            hlMemberRecordExport.setState("0".equals(hlMemberRecordDTO.getState())?"曾办理的会员":"现在的会员");
//            hlMemberRecordExport.setRemark(hlMemberRecordDTO.getRemark());
//            hlMemberRecordExport.setCreateBy(hlMemberRecordDTO.getCreateBy());
//            hlMemberRecordExport.setUpdateBy(hlMemberRecordDTO.getUpdateBy());
//            hlMemberRecordExport.setCreateTime(hlMemberRecordDTO.getCreateTime());
//            hlMemberRecordExport.setUpdateTime(hlMemberRecordDTO.getUpdateTime());

            //            ===========================================

//            将数据容器存放在传出的容器
            hlMemberRecordExports.add(hlMemberRecordExport);

        }

        return hlMemberRecordExports;
    }

    /**
     * 查询会员记录列表根据手机
     *
     * @param phone 会员记录
     * @return 会员记录集合
     */
    @Override
    public List<HlMemberRecordDTO> selectHlMemberRecordListByPhone(String phone) {
        List<HlMemberRecordDTO> hlMemberRecords = hlMemberRecordMapper.selectHlMemberRecordListByPhone(phone);



        return hlMemberRecords;
    }


    /**
     * 添加用户的业务
     *
     * @param hlUser 主键
     * @return 结果
     */
    @Override
    public AjaxResult addUser(HlUser hlUser) {

        if(Objects.isNull(hlUser)){
            return success("false","请填写要新增的用户信息");
        }
        HlUser user = hlUserMapper.selectHlUserByPhone(hlUser.getPhone());

        if(Objects.isNull(user)){
            String username = getUsername();
            hlUser.setCreateBy(username);
            hlUser.setUpdateBy(username);
            hlUser.setActiveTime(DateUtils.getNowDate());
            hlUser.setCreateTime(DateUtils.getNowDate());
            hlUser.setUpdateTime(DateUtils.getNowDate());
            int i = hlUserMapper.insertHlUser(hlUser);
            if (i < 1 ){
                return success("false","添加用户失败,请联系管理员");
            }

            HlUser u = hlUserMapper.selectHlUserByPhone(hlUser.getPhone());
            if(Objects.isNull(u)){
                return success("false","添加用户失败,请联系管理员");
            }
            Integer uId = u.getId();
            HlMemberRecord memberRecord = new HlMemberRecord();
            memberRecord.setUserId(uId);
            memberRecord.setMemberId(1);
            memberRecord.setState("1");
            memberRecord.setCreateBy(username);
            memberRecord.setUpdateBy(username);
            memberRecord.setCreateTime(DateUtils.getNowDate());
            memberRecord.setUpdateTime(DateUtils.getNowDate());
            int j = hlMemberRecordMapper.insertHlMemberRecord(memberRecord);

            if (j < 1){
                return success("false","添加用户失败,请联系管理员");
            }
            return success("true","添加用户成功，如需办理会员，请执行第二步：办理会员");
        }
        return success("false","添加失败,用户已存在");
    }

    /**
     * 办理会员的业务
     *
     * @param memberRecordVO 主键
     * @return 结果
     */
    @Override
    public AjaxResult addMember(MemberRecordVO memberRecordVO) {

//        ================================================

//        判断参数是否为空
        if(Objects.isNull(memberRecordVO)){
            return success("false","请填写要要办理会员的用户信息");
        }

//        ================================================

//        拿到参数的phone值
        String phone = memberRecordVO.getPhone();
//        通过phone找到当前用户，因为phone唯一
        HlUser hlUser = hlUserMapper.selectHlUserByPhone(phone);
//        判断当前用户是否为空
        if(Objects.isNull(hlUser)){
            return success("false","办理会员失败，如需办理会员，请先执行第一步：添加用户");
        }

//        ================================================

//        拿到参数的会员类型
        String memberType = memberRecordVO.getMemberType();
//        通过参数类型拿到会员表中 对应的那条数据
        HlMember hlMember = hlMemberMapper.selectHlMemberByMemberType(memberType);
//        判断拿到的会员表那条数据是否为空
        if(Objects.isNull(hlMember)){
            return success("false","办理会员失败，您未选择要办理的会员，请重新选择办理的会员");
        }
//        比较两个会员类型
        if("普通会员".equals(memberType)){
            return success("true","办理会员成功，该用户原本就是普通会员");
        }

//        ================================================

//        =============这里是真正执行添加会员业务的地方=========
//        获取当前操作的管理员名
        String username = getUsername();
//        获取当前操作的时间
        Date nowDate = DateUtils.getNowDate();
        Integer uId = hlUser.getId();
        Integer mId = hlMember.getId();
        HlMemberRecord hlMemberRecord = hlMemberRecordMapper.selectHlMemberRecordStateForOneByUserId(uId);
        Integer memberId = hlMemberRecord.getMemberId();
        Integer id = hlMemberRecord.getId();
//        如果能查到该数据且他的会员id为1就修改他的state为0，再新增数据
        if(1 == memberId){
            int j = hlMemberRecordMapper.updateHlMemberRecordStateForZeroByUserId(id,username,nowDate);
            if(j < 1){
                return success("false","办理会员失败，请重试 或 联系管理员");
            }

//        ================================================

//        创建当前表的空对象数据   将参数纷纷设置进去
            HlMemberRecord memberRecord = new HlMemberRecord();
            memberRecord.setUserId(uId);
            memberRecord.setMemberId(mId);
            memberRecord.setState("1");

            memberRecord.setCreateBy(username);
            memberRecord.setUpdateBy(username);
            memberRecord.setCreateTime(nowDate);
            memberRecord.setUpdateTime(nowDate);
            int i = hlMemberRecordMapper.insertHlMemberRecord(memberRecord);
            if(i < 1){
                return success("false","办理会员失败，请重试 或 联系管理员");
            }
            return success("true","办理会员成功");
        }
//        =================================================

        return success("false","办理会员失败，该用户已经是会员身份，无需添加会员，如需升级权限，请搜索该用户，再升级会员权限");
    }


    /**
     * 升级会员的业务
     *
     * @param memberRecordVO 主键
     * @return 结果
     */
    @Override
    public AjaxResult LevelMember(MemberRecordVO memberRecordVO) {

//        ================================================

//        判断参数是否为空
        if(Objects.isNull(memberRecordVO)){
            return success("false","请填写要要升级会员的用户信息");
        }

//        ================================================

//        拿到参数的phone值
        String phone = memberRecordVO.getPhone();
//        通过phone找到当前用户，因为phone唯一
        HlUser hlUser = hlUserMapper.selectHlUserByPhone(phone);
//        判断当前用户是否为空
        if(Objects.isNull(hlUser)){
            return success("false","升级会员失败，如需升级会员，请先执行第一步：添加用户");
        }

//        ================================================

//        拿到参数的会员类型
        String memberType = memberRecordVO.getMemberType();
//        通过参数类型拿到会员表中 对应的那条数据
        HlMember hlMember = hlMemberMapper.selectHlMemberByMemberType(memberType);
//        判断拿到的会员表那条数据是否为空
        if(Objects.isNull(hlMember)){
            return success("false","升级会员失败，您未选择要升级的会员，请重新选择升级的会员");
        }

//        ================================================

//        通过当前用户拿到他的userId
        Integer uId = hlUser.getId();
//        通过用户id查到 升级会员的 state为 1 的表  [以前的数据]
        HlMemberRecord hlMemberRecord = hlMemberRecordMapper.selectHlMemberRecordStateForOneByUserId(uId);

//        判断拿到的会员表那条数据是否为空
        if(Objects.isNull(hlMemberRecord)){
            return success("false","升级会员失败，未找到当前用户对应的会员信息，请重试 或 联系管理员");
        }

//        ================================================

//        通过升级会员的那条数据拿到他的memberId [以前的数据]
        Integer memberId = hlMemberRecord.getMemberId();
//        通过memberId拿到对应的Member表数据  [以前的数据]
        HlMember member = hlMemberMapper.selectHlMemberById(memberId);
//        通过这条数据拿到对应的价格  [以前的数据]
        Double memberPrice = member.getMemberPrice();
//        拿到参数对应的会员价格
        Double memberP = hlMember.getMemberPrice();
//        拿到的参数  小于数据库中的  参数  [通过价格判断是否是降权行为，即 要升级的价格如果比以前的价格更低]
        if(memberP < memberPrice){
            return success("false","当前是降级会员的行为，升级会员操作无权行使此操作");
        }

//        ================================================

//        拿到现在的要办理的会员id
        Integer mId = hlMember.getId();
//        以前的会员id 如果等于现在的会员id 就判断他俩会员是原本的会员，不需做操作
        if(memberId.equals(mId)){
            return success("true","升级会员成功，该用户原本就是该会员");
        }

//        =================================================

//        获取当前操作的管理员名
        String username = getUsername();
//        获取当前操作的时间
        Date nowDate = DateUtils.getNowDate();
//        通过这条数据拿到他的 升级会员id
        Integer id = hlMemberRecord.getId();
//        通过当前id修改当前这个id对应的这条数据修改state为0
        int i = hlMemberRecordMapper.updateHlMemberRecordStateForZeroByUserId(id,username,nowDate);
//        如果修改失败就报错
        if(i < 1){
            return success("false","升级会员失败，请重试 或 联系管理员");
        }

//        ================================================

//        创建当前表的空对象数据   将参数纷纷设置进去
        HlMemberRecord memberRecord = new HlMemberRecord();
        memberRecord.setUserId(uId);
        memberRecord.setMemberId(mId);
        memberRecord.setState("1");

        memberRecord.setCreateBy(username);
        memberRecord.setUpdateBy(username);
        memberRecord.setCreateTime(nowDate);
        memberRecord.setUpdateTime(nowDate);
        int j = hlMemberRecordMapper.insertHlMemberRecord(memberRecord);
        if(j < 1){
            return success("false","升级会员失败，请重试 或 联系管理员");
        }
        return success("true","升级会员成功");
    }








}
